/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.check.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.check.domain.CheckDetail;
import me.zhengjie.suli.check.repository.CheckDetailRepository;
import me.zhengjie.suli.check.service.CheckDetailService;
import me.zhengjie.suli.check.service.dto.CheckDetailDto;
import me.zhengjie.suli.check.service.dto.CheckDetailQueryCriteria;
import me.zhengjie.suli.check.service.mapstruct.CheckDetailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Allen Gao
* @date 2023-04-23
**/
@Service
@RequiredArgsConstructor
public class CheckDetailServiceImpl implements CheckDetailService {

    private final CheckDetailRepository checkDetailRepository;
    private final CheckDetailMapper checkDetailMapper;

    @Override
    public Map<String,Object> queryAll(CheckDetailQueryCriteria criteria, Pageable pageable){
        Page<CheckDetail> page = checkDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(checkDetailMapper::toDto));
    }

    @Override
    public List<CheckDetailDto> queryAll(CheckDetailQueryCriteria criteria){
        return checkDetailMapper.toDto(checkDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CheckDetailDto findById(String id) {
        CheckDetail checkDetail = checkDetailRepository.findById(id).orElseGet(CheckDetail::new);
        ValidationUtil.isNull(checkDetail.getId(),"CheckDetail","id",id);
        return checkDetailMapper.toDto(checkDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CheckDetailDto create(CheckDetail resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return checkDetailMapper.toDto(checkDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CheckDetail resources) {
        CheckDetail checkDetail = checkDetailRepository.findById(resources.getId()).orElseGet(CheckDetail::new);
        ValidationUtil.isNull( checkDetail.getId(),"CheckDetail","id",resources.getId());
        checkDetail.copy(resources);
        checkDetailRepository.save(checkDetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            checkDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CheckDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CheckDetailDto checkDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("主表ID", checkDetail.getCheckId());
            map.put("盘点明细编码", checkDetail.getCheckDetailCode());
            map.put("批次", checkDetail.getLotCode());
            map.put("库存数量", checkDetail.getQty());
            map.put("盘点数量", checkDetail.getCheckQty());
            map.put("盘差", checkDetail.getDifferentQty());
            map.put("盘点时间", checkDetail.getCheckTime());
            map.put("创建用户", checkDetail.getCreateBy());
            map.put("修改用户", checkDetail.getUpdateBy());
            map.put("创建日期", checkDetail.getCreateTime());
            map.put("修改日期", checkDetail.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<CheckDetail> getCheckDetailsByCheckId(String id) {
        return checkDetailRepository.getCheckDetailsByCheckId(id);
    }
}